package com.ytjj.qmyx.mall.utils;

import com.ytjj.common.exception.ApiException;

import java.text.DecimalFormat;
import java.util.Random;

public class RandomUtil {

    /**
     * 生成指定 min - max 之间的数量
     * @param max
     * @param min
     * @return
     */
    public static int getSectionNum(int min, int max){
        Random random = new Random();
        int s = random.nextInt(max)%(max-min+1) + min;
        return s;
    }

    /**
     * 浮点数
     * @param min
     * @param max
     * @return
     */
    public static double getNextDouble(final double min, final double max) {
        //保留两位小数
        DecimalFormat df = new DecimalFormat("#.00");
        if (max < min) {
            throw new ApiException("min < max");
        }
        if (min == max) {
            return min;
        }
        return  Double.parseDouble(df.format(min + ((max - min) * new  Random().nextDouble())));
    }


}
